package com.three.service.impl;


import com.baor.baseUtil.R;
import com.three.dao.LoginDao;
import com.three.domain.Users;
import com.three.service.LoginService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginDao loginDao;


    @Override
    public R login(String username, String password) {
        return R.ok(loginDao.login(username,password));
    }

    @Override
    public R logins(String username, String hashedPassword) {
        Users user = loginDao.logins(username);
        if(user == null) {
            return R.er();
        } else {
            String password = user.getPassword();
            boolean flag = BCrypt.checkpw(hashedPassword, password);
            user.setPassword(null);
            if (flag){
                return R.ok(user);
            }else {
                return R.er();
            }
        }

    }
}
